import {Component, OnInit, OnDestroy, OnChanges} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
import {NormalService} from '../../../apiServices/normal.service';
import { PageChangedEvent } from 'ngx-bootstrap/pagination';


@Component({
    selector: "transfer-history",
    templateUrl: './transfer-history.template.html'
})
export class TransferHistory implements OnInit {
    public language: string = 'en';
    private userSubscription: Subscription;
    public user: any={};
    public transfers: any;
    public returnedArray: any;
    public itemsPerPage: number = 10;
    public startItem: number = 0;
    constructor(
        private normalService: NormalService,
    ) {
        this.language = normalService.lang;
        this.userSubscription = normalService.user$.subscribe(user=>{
            this.user = user;
            normalService.findTransfer({query: {
                from: user._id,
                $sort: {createdAt: -1},
                $limit: this.itemsPerPage,
                $skip: this.startItem
            }}).then(transfers=>{
                this.transfers = transfers;
                console.log(transfers);
            });
        });
    }

    ngOnInit(): void {
        this.normalService.getuser();
    }

    pageChanged(event: PageChangedEvent): void {
        this.startItem = (event.page - 1) * this.itemsPerPage;
        console.log('startItem',this.startItem);
            this.normalService.findTransfer({query: {
                from: this.user._id,
                $sort: {createdAt: -1},
                $limit: this.itemsPerPage,
                $skip: this.startItem
            }}).then(transfers=>{
                this.transfers = transfers;
                console.log('transfercourent',this.transfers);
            });
      }
}